package org.blogwu.learn.leetcode.链表;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
public class _206_反转链表_递归 {

    public static void main(String[] args) {
        ListNode listNode5 = new ListNode(5);
        ListNode listNode4 = new ListNode(4, listNode5);
        ListNode listNode3 = new ListNode(3, listNode4);
        ListNode listNode2 = new ListNode(2, listNode3);
        ListNode listNode1 = new ListNode(1, listNode2);
        System.out.println(listNode1.toString());
        _206_反转链表_递归 ff = new _206_反转链表_递归();

        ListNode listNodeNew = ff.reverseList(listNode1);
        System.out.println(listNodeNew.toString());
    }

    //  1(2) >> 2(3) >> 3(4) >> 4(5) >> 5(null)
    //  1(null) << 2(1) << 3(2) << 4(3) << 5(4)
    public ListNode reverseList(ListNode head) {
        if (head == null) return null;
        if (head.next == null) return head;
        int i = 0;
        ListNode next = head.next;
        if (i == 0) {
            head.next = null;
        }
        return xxxx(head, next);
    }

    /**
     * 取上一次和下一次的
     *
     * @param pre
     * @param current
     * @return
     */
    public ListNode xxxx(ListNode pre, ListNode current) {
        ListNode next = current.next;
        current.next = pre;
        if (next == null) {
            return current;
        } else {
            return xxxx(current, next);
        }
    }

}
